package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Toast;

import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.WashOrderModel;
import com.pengcheng.taskapplication.model.WashTypeModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.mvp.view.fragment.DialogFragmentWashType;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;

public class WashCommitActivity extends BaseActivity implements IView,DialogFragmentWashType.OnFragmentInteractionListener {

    ArrayList<WashTypeModel> washTypeModelArrayList=new ArrayList<>();
    private UserPresenterV2 userPresenter;

    private String washid="";
    private String deviceid="";
    private String uid;
    private String newtoken;
    @Override
    protected int layoutResID() {
        return R.layout.activity_wash_commit_order_layout;
    }

    @Override
    protected void initData() {
        washid=getIntent().getStringExtra("washid");
        deviceid=getIntent().getStringExtra("deviceid");
        userPresenter = new UserPresenterV2(this);
        uid= SPUtils.getString(context,ConstantValue.SpType.userid);
        newtoken=SPUtils.getString(context,ConstantValue.SpType.newtoken);
        getwashoption(newtoken,uid);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.getwashoption)){
            ArrayList<WashTypeModel> list=(ArrayList<WashTypeModel>)data;
            washTypeModelArrayList.clear();
            washTypeModelArrayList.addAll(list);
        }
        if(actionType.equals(ConstantValue.RequestKey.createWashOrder)){
            WashOrderModel washOrderModel=(WashOrderModel)data;
            Intent intent = new Intent(context,WashConfrimOrderActivity.class);
            intent.putExtra("tradno",washOrderModel.getTradno());
            startActivity(intent);
            WashCommitActivity.this.finish();
        }
    }


    @Override
    public void onFailed(String code, String msg) {

    }
    void showDialog(ArrayList<WashTypeModel> washTypeModelArrayList) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        // Create and show the dialog.
        DialogFragmentWashType newFragment = DialogFragmentWashType.newInstance(washTypeModelArrayList);
        newFragment.show(ft, "dialog");
    }

    @OnClick({R.id.img_back,R.id.ll_select})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_select:
                if(washTypeModelArrayList.size()!=0) {
                    showDialog(washTypeModelArrayList);
                }
                break;
        }
    }

    public void getwashoption(String token,String uid){
        userPresenter.getwashoption(token,uid,ConstantValue.RequestKey.getwashoption);
    }

    @Override
    public void onCancel() {

    }

    @Override
    public void onDelete() {

    }



    @Override
    public void onCompete(WashTypeModel model) {
        //监听选择哪个价格  下单，调用生成订单的接口，根据订单的返回，跳转到确认页面
        if(washid.length()!=0&&deviceid.length()!=0) {
            createWashOrder(newtoken, uid, washid, deviceid, model.getOptionkey());
        }
    }
    public void createWashOrder(String token,String uid,String washid,String deviceid,String washtype){
        userPresenter.createWashOrder(token,uid,washid,deviceid,washtype,ConstantValue.RequestKey.createWashOrder);
    }
}
